package com.chushouya.product.service.api.impl;

import com.general.framework.core.enums.StatusEnum;
import com.general.framework.core.lang.Beans;
import com.general.framework.core.schema.PageResult;
import com.general.framework.data.redis.RedisCache;
import com.chushouya.product.dao.entity.ProductEntity;
import com.chushouya.product.dao.repository.ProductRepository;
import com.chushouya.product.dto.admin.product.query.ProductQuery;
import com.chushouya.product.dto.api.product.ProductApiQuery;
import com.chushouya.product.dto.api.product.ProductListApiDTO;
import com.chushouya.product.service.api.ProductApiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/**
 * @author CodeGenerator
 * @data 2022/05/28.
 */
@Slf4j
@Service
public class ProductApiServiceImpl implements ProductApiService {
    @Resource
    private ProductRepository productRepository;

    @Resource
    private RedisCache redisCache;


    @Override
    public List<ProductListApiDTO> search(ProductApiQuery query) {
        ProductQuery productQuery = Beans.copy(query, ProductQuery.class);
        productQuery.setStatus(StatusEnum.normal.value());
        List<ProductEntity> productList = productRepository.selectList(productQuery);
        return Beans.copyList(productList, ProductListApiDTO.class);
    }

    @Override
    public PageResult<ProductListApiDTO> selectPage(ProductApiQuery query) {
//        String key = Strings.format("{}:{}_{}_{}_{}_{}", PRODUCT_LIST, query.getCategoryId(), query.getBrandId(), query.getSeriesId(), query.getPage(), query.getSize());
//        PageResult<ProductListApiDTO> pageResult = redisCache.getCacheObject(key);
//        if (Objects.nonNull(pageResult)) {
//            return pageResult;
//        }
        ProductQuery productQuery = Beans.copy(query, ProductQuery.class);
        productQuery.setStatus(StatusEnum.normal.value());
        PageResult<ProductEntity> result = productRepository.selectPage(productQuery);
        PageResult<ProductListApiDTO> pageResult = PageResult.of(result, ProductListApiDTO.class);
//        redisCache.setCacheObject(key, pageResult, 1L, TimeUnit.DAYS);
        return pageResult;
    }

}
